//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props:{item:{},index:0},
    template:[
        '<li :class="{active: (item.type===0 && index === 0)}">',
        '<a v-if="item.type === 0" href="javascript:;">',
        '<i v-if="item.icon != null" :class="item.icon"></i>',
        '<span>{{item.name}}</span>',
        '<i class="fa fa-angle-left pull-right"></i>',
        '</a>',
        '<ul v-if="item.type === 0" class="treeview-menu">',
        '<menu-item :item="item" :index="index" v-for="(item, index) in item.list"></menu-item>',
        '</ul>',
        '<a v-if="item.type === 1" :href="\'#\'+item.url">' +
        '<i v-if="item.icon != null" :class="item.icon"></i>' +
        '<i v-else class="fa fa-circle-o"></i> {{item.name}}' +
        '</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function() {
	var $content = $('.content');
	$content.height($(this).height() - 120);
	$content.find('iframe').each(function() {
		$(this).height($content.height());
	});
}).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);

var vm = new Vue({
	el:'#rrapp',
	data:{
		user:{},
		menuList:{},
        preBottom:{bottomName:''},
        currentBottom:{bottomName:''},
		main:"modules/sys/bottom.html",
		password:'',
		newPassword:'',
        navTitle:"首页",
		payPassword:'',
		amount:0,
        bankCard:'',
        openBank:'',
        realName:'',
        showTime:{
            awardRemainTime:-1,//开奖剩余时间
            endRemainTime:-1,//封盘剩余时间
            awardRemainTimeMinuteShow:'00',//页面展示的开奖剩余分钟
            awardRemainTimeSecondShow:'00',//页面展示的开奖剩余秒
            endRemainTimeMinuteShow:'00',//页面展示的封盘剩余分钟
            endRemainTimeSecondShow:'00'//页面展示的封盘剩余分钟
        }
	},
	methods: {
		getMenuList: function () {
			$.getJSON(baseURL + "sys/menu/nav", function(r){
				vm.menuList = r.menuList;
                window.permissions = r.permissions;
			});
		},
		getUser: function(){
			$.getJSON(baseURL + "sys/user/info", function(r){
				vm.user = r.user;
				vm.realName = vm.user.realName;
				vm.bankCard = vm.user.bankCard;
				vm.openBank = vm.user.openBank;
			});
		},
		getPreBottom: function () {
            $.getJSON(baseURL + "sys/sysbottom/getPreBottom", function(r){
                vm.preBottom = r.preBottom;
            });
            setInterval(function () {
                $.getJSON(baseURL + "sys/sysbottom/getPreBottom", function(r){
                    if (vm.preBottom.bottomName != r.preBottom.bottomName) {
                        vm.preBottom = r.preBottom;
                        vm.getUser();
                    }
                });
            }, 10000);
        },
		getCurrentBottom: function () {
            $.getJSON(baseURL + "sys/sysbottom/getCurrentBottom", function(r){
                vm.currentBottom = r.currentBottom == null ? vm.currentBottom : r.currentBottom;
                if (vm.currentBottom == null) {
                    return;
                }
                var currentDate = new Date();
                var createTime = new Date(vm.currentBottom.createTime);
                var alreadyTime = currentDate.getTime() - createTime.getTime();

                var duration = vm.currentBottom.duration * 60;

                vm.showTime.awardRemainTime = duration - (alreadyTime / 1000);
                vm.showTime.endRemainTime = vm.showTime.awardRemainTime - 45;
                setInterval(function() {
                    vm.showTime.awardRemainTime--;
                    vm.showTime.endRemainTime--;

                    vm.showTime.awardRemainTimeMinuteShow = formatTime(Math.floor(vm.showTime.awardRemainTime/60%60));
                    vm.showTime.awardRemainTimeSecondShow = formatTime(Math.floor(vm.showTime.awardRemainTime%60));

                    vm.showTime.endRemainTimeMinuteShow = formatTime(Math.floor(vm.showTime.endRemainTime/60%60));
                    vm.showTime.endRemainTimeSecondShow = formatTime(Math.floor(vm.showTime.endRemainTime%60));
                }, 1000);
            });
        },
		drawMoney: function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "在线提款",
                area: ['550px', '400px'],
                shadeClose: false,
                content: jQuery("#drawMoneyLayer"),
                btn: ['确定','取消'],
                btn1: function (index) {
                    var data = "payPassword="+vm.payPassword+"&amount="+vm.amount+"&realName="+vm.realName
                                                                +"&bankCard="+vm.bankCard+"&openBank="+vm.openBank;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/sysextract/save",
                        data: data,
                        dataType: "json",
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                layer.alert('提现成功', function(){
                                    location.reload();
                                });
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
		updatePassword: function(){
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "修改密码",
				area: ['550px', '270px'],
				shadeClose: false,
				content: jQuery("#passwordLayer"),
				btn: ['修改','取消'],
				btn1: function (index) {
					var data = "password="+vm.password+"&newPassword="+vm.newPassword;
					$.ajax({
						type: "POST",
					    url: baseURL + "sys/user/password",
					    data: data,
					    dataType: "json",
					    success: function(r){
							if(r.code == 0){
								layer.close(index);
								layer.alert('修改成功', function(){
									location.reload();
								});
							}else{
								layer.alert(r.msg);
							}
						}
					});
	            }
			});
		},
        logout: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/logout",
                dataType: "json",
                success: function(r){
                    //删除本地token
                    localStorage.removeItem("token");
                    //跳转到登录页面
                    location.href = baseURL + 'login.html';
                }
            });
        },
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        }
	},
	created: function(){
		this.getMenuList();
		this.getUser();
		this.getPreBottom();
		this.getCurrentBottom();
	},
	updated: function(){
		//路由
		var router = new Router();
		routerList(router, vm.menuList);
		router.start();
	}
});



function routerList(router, menuList){
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.type == 0){
			routerList(router, menu.list);
		}else if(menu.type == 1){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
			    vm.main = url.replace('#', '');
			    
			    //导航菜单展开
			    $(".treeview-menu li").removeClass("active");
                $(".sidebar-menu li").removeClass("active");
			    $("a[href='"+url+"']").parents("li").addClass("active");
			    
			    vm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}

function formatTime(time) {
    if (time < 10) {
        return '0' + time;
    }else {
        return time;
    }
}